package com.oig.sys.base.app.impl;


import cn.hutool.core.util.ObjectUtil;
import com.oig.common.constants.CommonEnum;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.base.app.IAppHsGoodsTariffService;
import com.oig.sys.base.dto.HsGoodsTariffDto;
import com.oig.sys.base.msg.IDepMqMsgService;
import com.oig.sys.base.service.ITbCountryGroupService;
import com.oig.sys.base.service.ITbHsGoodsService;
import com.oig.sys.base.service.ITbHsGoodsTariffService;
import com.oig.sys.base.vo.CountryGroupMemberVo;
import com.oig.sys.base.vo.CountryGroupVo;
import com.oig.sys.base.vo.HsGoodsTariffVo;
import com.oig.sys.base.vo.HsGoodsVo;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class AppHsGoodsTariffServiceImpl implements IAppHsGoodsTariffService {

    private final ITbHsGoodsTariffService hsGoodsTariffService;
    private final ITbCountryGroupService countryGroupService;
    private final ITbHsGoodsService hsGoodsService;
    private final IDepMqMsgService depMqMsgService;

    @GlobalTransactional
    @Override
    public List<HsGoodsTariffVo> addGroupHsGoodsTariff(HsGoodsTariffDto dto) {
        CountryGroupVo countryGroupVo = this.countryGroupService.getCountryGroup(dto.getCountryGroupId());
        if (countryGroupVo==null){
            throw new BadRequestException("国别组id错误") ;
        }
        List<HsGoodsTariffVo> tariffVoList = new ArrayList<>();
        for (CountryGroupMemberVo countryVo : countryGroupVo.getCountryList()){
            HsGoodsTariffDto hsGoodsTariffDto = new HsGoodsTariffDto();
            hsGoodsTariffDto.setHsCode(dto.getHsCode());
            hsGoodsTariffDto.setCountryCode(countryVo.getCode());
            hsGoodsTariffDto.setCountryApplyCode(countryVo.getApplyCode());
            hsGoodsTariffDto.setCountryName(countryVo.getName());
            hsGoodsTariffDto.setTariffRate(dto.getTariffRate());
            hsGoodsTariffDto.setTariffType(dto.getTariffType());
            hsGoodsTariffDto.setRemark(dto.getRemark());
            hsGoodsTariffDto.setCountryGroupId(dto.getCountryGroupId());
            hsGoodsTariffDto.setCountryGroupName(dto.getCountryGroupName());
            hsGoodsTariffDto.setLastUpdateId(dto.getLastUpdateId());
            hsGoodsTariffDto.setLastUpdateName(dto.getLastUpdateName());
            if (dto.getPriority()==null){
                hsGoodsTariffDto.setPriority(0);
            } else {
                hsGoodsTariffDto.setPriority(dto.getPriority());
            }
            HsGoodsTariffVo hsGoodsTariffVo = this.hsGoodsTariffService.addOrUpdateTariff(hsGoodsTariffDto);
            tariffVoList.add(hsGoodsTariffVo);
        }
        // mq推送
        this.sendMsg(dto.getHsCode());
        return tariffVoList;
    }


    @GlobalTransactional
    @Override
    public HsGoodsTariffVo addHsGoodsTariff(HsGoodsTariffDto hsGoodsTariffDto) {
        HsGoodsTariffVo hsGoodsTariffVo = hsGoodsTariffService.addHsGoodsTariff(hsGoodsTariffDto);
        // mq推送
        this.sendMsg(hsGoodsTariffVo.getHsCode());
        return hsGoodsTariffVo;
    }

    @GlobalTransactional
    @Override
    public HsGoodsTariffVo updateHsGoodsTariff(HsGoodsTariffDto hsGoodsTariffDto) {
        if(!this.matchInfo(hsGoodsTariffDto.getCountryGroupId(), hsGoodsTariffDto.getCountryCode())){
            hsGoodsTariffDto.setCountryGroupId(null);
            hsGoodsTariffDto.setCountryGroupName(null);
        }
        HsGoodsTariffVo hsGoodsTariffVo = hsGoodsTariffService.updateHsGoodsTariff(hsGoodsTariffDto);
        // mq推送
        this.sendMsg(hsGoodsTariffVo.getHsCode());
        return hsGoodsTariffVo;
    }

    @GlobalTransactional
    @Override
    public void delHsGoodsTariff(Integer id) {
        String code = hsGoodsTariffService.delHsGoodsTariff(id);
        this.sendMsg(code);
    }

    @Override
    public HsGoodsTariffVo getTariff(String countryCode, String hsCode) {
        return hsGoodsTariffService.getTariff(countryCode, hsCode);
    }

    @Override
    public List<HsGoodsTariffVo> getHsGoodsAllTariff(String hsCode) {
        return hsGoodsTariffService.getHsGoodsAllTariff(hsCode);
    }

    @Override
    public HsGoodsTariffVo getHsGoodsTariffInfo(Integer id) {
        return hsGoodsTariffService.getHsGoodsTariffInfoById(id);
    }

    /**
     * 是否在国别组内
     * @param countryGroupId 国别组id
     * @param countryCode 国家code
     * */
    private boolean matchInfo(Integer countryGroupId, String countryCode){
        boolean flag = false;
        if(ObjectUtil.isEmpty(countryGroupId)){
            return false;
        }
        CountryGroupVo countryGroupVo = this.countryGroupService.getCountryGroup(countryGroupId);
        if(!ObjectUtil.isEmpty(countryGroupVo)){
            for(CountryGroupMemberVo countryGroupMemberVo : countryGroupVo.getCountryList()){
                if (countryGroupMemberVo.getCode().equals(countryCode)) {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    public void sendMsg(String hsCode) {
        HsGoodsVo hsGoodsVo = hsGoodsService.getHsGoods(hsCode);
        if(ObjectUtil.isEmpty(hsGoodsVo)){
            log.info("查无海关商品信息");
            return;
        }
        List<HsGoodsTariffVo> hsGoodsTariffVoList = hsGoodsTariffService.getHsGoodsAllTariff(hsGoodsVo.getHsCode());
        hsGoodsVo.setHsGoodsTariffList(hsGoodsTariffVoList);
        // 发送mq
        depMqMsgService.addToSendMsg(CommonEnum.RelationType.OIGX_HS_GOODS.getCode(), hsGoodsVo.getHsCode(),hsGoodsVo, 0, "系统服务");
    }


    @Override
    public List<HsGoodsTariffVo> getTariffByHSCodeAndCountryCodeList(List<HsGoodsTariffDto> req) {
        List<HsGoodsTariffVo> list = new ArrayList<>();
        for (HsGoodsTariffDto hsGoodsTariffDto : req) {
            String hsCode = hsGoodsTariffDto.getHsCode();
            String countryCode = hsGoodsTariffDto.getCountryCode();

            if (hsCode == null || countryCode == null) {
                continue;
            }

            HsGoodsTariffVo tariff = hsGoodsTariffService.getTariff(countryCode, hsCode);
            list.add(tariff);
        }
        return list;
    }
}
